package handler

import (
	"week-exam-sep18/model"

	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")

	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "标题和内容不能为空",
		})
		return
	}

	email := model.Email{
		Title:   title,
		Content: content,
	}
	err := model.DB.Create(&email).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	var email []model.Email
	model.DB.Find(&email).Debug()

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": email,
	})
	return
}
